package com.phone580.app.download;

import java.io.File;
import java.util.List;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.phone580.app.CommonData;
import com.phone580.app.R;
import com.phone580.app.entity.DownloadInfoEntity;
import com.phone580.app.model.DownloadManagerModel;
import com.phone580.app.untilty.CommonUtility;

public class FinishDownloadAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<DownloadInfoEntity> data;
	private Context context;
	private NotificationManager nm;
	
	public FinishDownloadAdapter (Context context) {
		this.context = context;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	public void setData(List<DownloadInfoEntity> data) {
		this.data = data;
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public DownloadInfoEntity getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder holder = null;
		if (v == null) {
			holder = new ViewHolder();
			v = inflater.inflate(R.layout.finish_down_item, null);
			holder.tvSize = (TextView)v.findViewById(R.id.finish_item_size_tv);
			holder.tvTitle = (TextView)v.findViewById(R.id.finish_item_title_tv);
			holder.btnInstall = (TextView) v.findViewById(R.id.finish_item_install_tv);
			holder.btnDelete = (TextView) v.findViewById(R.id.finish_item_delete_tv);
			holder.ivIcon = (ImageView)v.findViewById(R.id.finish_down_item_icon_tv);
			v.setTag(holder);
		}
		holder = (ViewHolder) v.getTag();
		final DownloadInfoEntity entity = data.get(position);
		holder.tvTitle.setText(entity.getFileName());
		
		holder.tvSize.setText(entity.getFileSize() / 1024 + "kb");
		
		Drawable icon = CommonUtility.getUninstallAPKIcon(context, entity.getFilePath());
		if (icon != null) {
			holder.ivIcon.setImageDrawable(icon);
		} 
		holder.btnInstall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Integer notifyId = CommonData.notificationId.get(entity.getFileName());
				if (notifyId != null) {
					nm.cancel(notifyId);
				}
				installApk(entity.getFilePath());
			}
		});
		holder.btnDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				File file = new File(entity.getFilePath());
				if (file.exists()) {
					file.delete();
					Integer notifyId = CommonData.notificationId.get(entity.getFileName());
					if (notifyId != null) {
						nm.cancel(notifyId);
					}
				}
				DownloadManagerModel model = new DownloadManagerModel(context);
				model.remove(entity.getId());
				((FinishDownloadActivity)context).refresh();
			}
		});
		return v;
	}
	
	private void installApk(String filename) {
		File file = new File(filename);
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
	    intent.setAction(android.content.Intent.ACTION_VIEW);  
	    String type = "application/vnd.android.package-archive";
	    /* 设置intent的file与MimeType */  
	    intent.setDataAndType(Uri.fromFile(file),type);  
	    context.startActivity(intent);   
	}
	
	class ViewHolder {
		public TextView tvTitle;
		public TextView tvSize;
		public ImageView ivIcon;
		public TextView btnInstall;
		public TextView btnDelete;
	}

}
